﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using OF.Session;
namespace OF.DAL
{
	/// <summary>
	/// 数据访问类:Dish
	/// </summary>
    public partial class Dish : Session.DALBase
	{
		public Dish()
		{}
		#region  BasicMethod
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Dish");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(OF.Model.Dish model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Dish(");
			strSql.Append("dishname,catid,state,describe,photo,hashot,hasnew,praisetimes,price,ordertimes,addtime,modifytimes,sellerid)");
			strSql.Append(" values (");
			strSql.Append("@dishname,@catid,@state,@describe,@photo,@hashot,@hasnew,@praisetimes,@price,@ordertimes,@addtime,@modifytimes,@sellerid)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@dishname", SqlDbType.NVarChar,20),
					new SqlParameter("@catid", SqlDbType.Int,4),
					new SqlParameter("@state", SqlDbType.TinyInt,1),
					new SqlParameter("@describe", SqlDbType.NVarChar,-1),
					new SqlParameter("@photo", SqlDbType.NVarChar,200),
					new SqlParameter("@hashot", SqlDbType.Bit,1),
					new SqlParameter("@hasnew", SqlDbType.Bit,1),
					new SqlParameter("@praisetimes", SqlDbType.Int,4),
					new SqlParameter("@price", SqlDbType.Decimal,9),
					new SqlParameter("@ordertimes", SqlDbType.Int,4),
					new SqlParameter("@addtime", SqlDbType.DateTime),
					new SqlParameter("@modifytimes", SqlDbType.DateTime),
					new SqlParameter("@sellerid", SqlDbType.Int,4)};
			parameters[0].Value = model.dishname;
			parameters[1].Value = model.catid;
			parameters[2].Value = model.state;
			parameters[3].Value = model.describe;
			parameters[4].Value = model.photo;
			parameters[5].Value = model.hashot;
			parameters[6].Value = model.hasnew;
			parameters[7].Value = model.praisetimes;
			parameters[8].Value = model.price;
			parameters[9].Value = model.ordertimes;
			parameters[10].Value = model.addtime;
			parameters[11].Value = model.modifytimes;
			parameters[12].Value = model.sellerid;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(OF.Model.Dish model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Dish set ");
			strSql.Append("dishname=@dishname,");
			strSql.Append("catid=@catid,");
			strSql.Append("state=@state,");
			strSql.Append("describe=@describe,");
			strSql.Append("photo=@photo,");
			strSql.Append("hashot=@hashot,");
			strSql.Append("hasnew=@hasnew,");
			strSql.Append("praisetimes=@praisetimes,");
			strSql.Append("price=@price,");
			strSql.Append("ordertimes=@ordertimes,");
			strSql.Append("addtime=@addtime,");
			strSql.Append("modifytimes=@modifytimes,");
			strSql.Append("sellerid=@sellerid");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@dishname", SqlDbType.NVarChar,20),
					new SqlParameter("@catid", SqlDbType.Int,4),
					new SqlParameter("@state", SqlDbType.TinyInt,1),
					new SqlParameter("@describe", SqlDbType.NVarChar,-1),
					new SqlParameter("@photo", SqlDbType.NVarChar,200),
					new SqlParameter("@hashot", SqlDbType.Bit,1),
					new SqlParameter("@hasnew", SqlDbType.Bit,1),
					new SqlParameter("@praisetimes", SqlDbType.Int,4),
					new SqlParameter("@price", SqlDbType.Decimal,9),
					new SqlParameter("@ordertimes", SqlDbType.Int,4),
					new SqlParameter("@addtime", SqlDbType.DateTime),
					new SqlParameter("@modifytimes", SqlDbType.DateTime),
					new SqlParameter("@sellerid", SqlDbType.Int,4),
					new SqlParameter("@id", SqlDbType.Int,4)};
			parameters[0].Value = model.dishname;
			parameters[1].Value = model.catid;
			parameters[2].Value = model.state;
			parameters[3].Value = model.describe;
			parameters[4].Value = model.photo;
			parameters[5].Value = model.hashot;
			parameters[6].Value = model.hasnew;
			parameters[7].Value = model.praisetimes;
			parameters[8].Value = model.price;
			parameters[9].Value = model.ordertimes;
			parameters[10].Value = model.addtime;
			parameters[11].Value = model.modifytimes;
			parameters[12].Value = model.sellerid;
			parameters[13].Value = model.id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Dish ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Dish ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public OF.Model.Dish GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 id,dishname,catid,state,describe,photo,hashot,hasnew,praisetimes,price,ordertimes,addtime,modifytimes,sellerid from Dish ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			OF.Model.Dish model=new OF.Model.Dish();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public OF.Model.Dish DataRowToModel(DataRow row)
		{
			OF.Model.Dish model=new OF.Model.Dish();
			if (row != null)
			{
				if(row["id"]!=null && row["id"].ToString()!="")
				{
					model.id=int.Parse(row["id"].ToString());
				}
				if(row["dishname"]!=null)
				{
					model.dishname=row["dishname"].ToString();
				}
				if(row["catid"]!=null && row["catid"].ToString()!="")
				{
					model.catid=int.Parse(row["catid"].ToString());
				}
				if(row["state"]!=null && row["state"].ToString()!="")
				{
					model.state=int.Parse(row["state"].ToString());
				}
				if(row["describe"]!=null)
				{
					model.describe=row["describe"].ToString();
				}
				if(row["photo"]!=null)
				{
					model.photo=row["photo"].ToString();
				}
				if(row["hashot"]!=null && row["hashot"].ToString()!="")
				{
					if((row["hashot"].ToString()=="1")||(row["hashot"].ToString().ToLower()=="true"))
					{
						model.hashot=true;
					}
					else
					{
						model.hashot=false;
					}
				}
				if(row["hasnew"]!=null && row["hasnew"].ToString()!="")
				{
					if((row["hasnew"].ToString()=="1")||(row["hasnew"].ToString().ToLower()=="true"))
					{
						model.hasnew=true;
					}
					else
					{
						model.hasnew=false;
					}
				}
				if(row["praisetimes"]!=null && row["praisetimes"].ToString()!="")
				{
					model.praisetimes=int.Parse(row["praisetimes"].ToString());
				}
				if(row["price"]!=null && row["price"].ToString()!="")
				{
					model.price=decimal.Parse(row["price"].ToString());
				}
				if(row["ordertimes"]!=null && row["ordertimes"].ToString()!="")
				{
					model.ordertimes=int.Parse(row["ordertimes"].ToString());
				}
				if(row["addtime"]!=null && row["addtime"].ToString()!="")
				{
					model.addtime=DateTime.Parse(row["addtime"].ToString());
				}
				if(row["modifytimes"]!=null && row["modifytimes"].ToString()!="")
				{
					model.modifytimes=DateTime.Parse(row["modifytimes"].ToString());
				}
				if(row["sellerid"]!=null && row["sellerid"].ToString()!="")
				{
					model.sellerid=int.Parse(row["sellerid"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,dishname,catid,state,describe,photo,hashot,hasnew,praisetimes,price,ordertimes,addtime,modifytimes,sellerid ");
			strSql.Append(" FROM Dish ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" id,dishname,catid,state,describe,photo,hashot,hasnew,praisetimes,price,ordertimes,addtime,modifytimes,sellerid ");
			strSql.Append(" FROM Dish ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Dish ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from Dish T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Dish";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return session.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod


		#region  ExtensionMethod

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPages(string strWhere, string orderby, int pageIndex, int pageSize)
        {
            return GetListByPage("Dish", "id", strWhere, orderby, pageIndex, pageSize);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<OF.Model.Dish> GetModelList(string strWhere)
        {
            DataSet ds = GetList(strWhere); if (ds != null && ds.Tables[0].Rows.Count > 0) { if (ds != null && ds.Tables[0].Rows.Count > 0) { return DataTableToList(ds.Tables[0]); } else { return null; } } else { return null; }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<OF.Model.Dish> DataTableToList(DataTable dt)
        {
            List<OF.Model.Dish> modelList = new List<OF.Model.Dish>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                OF.Model.Dish model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }
		#endregion  ExtensionMethod
	}
}

